home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15434 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  74 lines

  1. Path: solaris.cc.vt.edu!FQDN!usenet
  2. From: quartic@polloux.fciencias.unam.mx (Mena Quintero Federico)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Q: Newbie using text files
  5. Date: 18 Apr 1996 20:43:41 -0500
  6. Organization: Universidad Nacional Autonoma de Mexico
  7. Sender: quartic@polloux.fciencias.unam.mx
  8. Message-ID: <soenpl7zua.fsf@polloux.fciencias.unam.mx>
  9. References: <4l6aqu$rfr@chile.it.earthlink.net>
  10. Reply-To: quartic@polloux.fciencias.unam.mx
  11. NNTP-Posting-Host: polloux.fciencias.unam.mx
  12. In-reply-to: brunop@earthlink.net's message of Thu, 18 Apr 1996 20:04:46 GMT
  13. X-Newsreader: Gnus v5.1
  14.  
  15. >    use a betting langauage if I could.  But, trying to input and
  16. >    processes text files in C seems a lot harder than in C, what with
  17. >    pointers and all.
  18.  
  19. They say you can make your C programs as complex as you wish, so C
  20. being harder than C itself *could* be reasonable... ;)
  21.  
  22. >    My point, and I do have one, is it worth my time to learn C to process
  23. >    ASCII text files with fixed lenth fields, for sorting etc...
  24.  
  25. For one thing, your program will be much faster, and (if written
  26. properly) it will be portable.  Anyway, if you *only* want to process
  27. text files, why not use Perl?
  28.  
  29. >    open "foo.bar" for input as #1
  30. >    open "foo.out" for output as #2
  31. >    while not eof(1)
  32. >        line input #1, row
  33. >        FName = mid$(row, 1, 25)    'characters 1-25 are first name
  34. >        LName = mid$(row, 26, 50)
  35. >        test = mid$(LName, 26, 1)
  36. >        if test < "M" then print #2, row    'if person's last name begins with
  37. >    character less then M print to output file, else we don't want them!
  38. >    wend
  39. >    close #1, #2
  40.  
  41. Argh!  BASIC in c.l.c!!!  OK, so here it goes:
  42.  
  43.  
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47.  
  48. #define MAX_LEN   100
  49.  
  50. int main(void)
  51. {
  52.     FILE *input;
  53.     FILE *output;
  54.     char  buf[MAX_LEN];
  55.  
  56.     input  = fopen("foo.in", "rt");
  57.     output = fopen("foo.out", "rt);
  58.  
  59.     if (!input || !output)
  60.         exit(1);
  61.  
  62.     while (fgets(buf, MAX_LEN, input) 
  63.         if (buf[25] < 'M')   /* It's always the 26th char, isn't it? */
  64.             fputs(buf, output);
  65.  
  66.     fclose(input);
  67.     fclose(output);
  68. } /* main */
  69.  
  70.  
  71. Hope this helps.
  72.  
  73.   Quartic
  74.